package com.sysu.ooad.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.sysu.ooad.dao.CommonDao;
import com.sysu.ooad.dao.TeacherDao;
import com.sysu.ooad.entity.Course;
import com.sysu.ooad.entity.Teacher;

@Component("teacherDao")
public class TeacherDaoImpl extends CommonDao implements TeacherDao {

	@SuppressWarnings("unchecked")
	public Teacher findByName(String name) {
		List<Teacher> teachers = (List<Teacher>)hibernateTemplate.find("from Teacher t where t.name = ?", new Object[]{name});
		if(teachers.isEmpty()) return null;
		return teachers.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Teacher> findAssistantsByCourse(Course c) {
		List<Teacher> assistants = (List<Teacher>)hibernateTemplate.find("from Teacher t where t.course = ? and t.priviledge = ?", 
				new Object[]{c, Teacher.Priviledge.ASSISTANT});
		return assistants;
	}

	public Teacher add(Teacher t) {
		hibernateTemplate.save(t);
		return t;
	}
}
